package com.google.code.jmarias.client;

import com.google.code.jmarias.client.phases.AbstractPhase;
import com.google.code.jmarias.common.Card;
import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;



/**
 * Trida reprezentujici vytvoreny stul. Obsahuje vsechny mozne promenne a vypocty co se budou v prubehu vykreslovani potrebovat.
 *
 * @author bart
 * @copiedfrom pepa ;)
 */
public class GameLocals {
    private static GameLocals table = null;

    private Globals global;

    private AbstractPhase currentPhase;

    private ArrayList<Card> cards = new ArrayList<Card>();

    /* Inicializace entit */
    /* Vrstva - pozadi */
    private Entity backgroundLayer = null;

    // Vrstva - Informacni vypisy
    private Entity infoLayer = null;


    // Vybiraci ramecek
    private Entity frameEntity = null;

    // Ruka, obecne
    protected Entity handEntity = null;

    // Entity karet v ruce
    private ArrayList<Entity> cardsEntities = new ArrayList<Entity>();

    private ArrayList<Entity> hidCardsEntities = new ArrayList<Entity>();

    
    private Card trumpCard = null;

    private int wait = 0;

    // Meritko karet v ruce
    private static float handScale = 0.7f;

    // Pozice stredu oblouku karet
    private static Vector2f handPosition;

    // Maximalní pocet karet v hracove ruce. Na velikosti zalezi rozestupy mezi kartami
    // Musi byt liche cislo!
    private static final int maxCards = 23;

    // Pozice jednotlivych karet - pocitaji se zvlast pro sudy a lichy pocet karet v ruce
    private Vector2f [] cardPositionOdd  = new Vector2f[maxCards];
    private Vector2f [] cardPositionEven  = new Vector2f[maxCards-1];

    // Hracem vybrane karty
    private ArrayList<Entity> pickedCards = new ArrayList<Entity>();


    public GameLocals(Globals global) throws SlickException {
        System.out.println("Incializace stolu");

        this.global = global;

        handPosition = new Vector2f(global.getScreenWidth() / 2f - (65f * handScale) , global.getScreenHeight());



        // Vypocet pozic pro lichy pocet karet v ruce
        for(int i=0; i<maxCards; i++) {
            cardPositionOdd[i] = new Vector2f(400 * (float) Math.cos((double) i / (float)(22 / Math.PI)),
                                           300 * (float) Math.sin((double) i / (float)(22 / Math.PI)));
        }

        // Vypocet pozic pro sudy pocet karet v ruce
        for(int i=0; i< maxCards - 1; i++) {
            cardPositionEven[i] = new Vector2f(400 * (float) Math.cos((double) i / (float)(21 / Math.PI)),
                                           300 * (float) Math.sin((double) i / (float)(21 / Math.PI)));
        }
    }

  
    public AbstractPhase getCurrentPhase() {
        return currentPhase;
    }

    public void setCurrentPhase(AbstractPhase currentPhase) {
        this.currentPhase = currentPhase;
    }



    public Vector2f getHandPosition() {
        return handPosition;
    }

    public void setHandPosition(Vector2f handPosition) {
        GameLocals.handPosition = handPosition;
    }

    public float getHandScale() {
        return handScale;
    }

    public void setHandScale(float handScale) {
        GameLocals.handScale = handScale;
    }


    /**
     * Vrati pole pozic jednotlivych karet v ruce v zavislosti na jejich poctu a pozici ruky.
     *
     * @param numOfCards Pocet karet pro ktere chceme pozice.
     * @param handPosition Pozice stredu vejire v ruce.
     */

    public ArrayList<Vector2f> getPositionsInHand(int numOfCards, Vector2f handPosition) {
        ArrayList<Vector2f>cardPositions = new ArrayList<Vector2f>();


        if(numOfCards % 2 == 1) {
            for(int i = 0; i < numOfCards; i++ ) {
                cardPositions.add(new Vector2f(handPosition.x - cardPositionOdd[Math.round((23-numOfCards)/2) + i].x,
                                                handPosition.y - cardPositionOdd[Math.round((23-numOfCards)/2) + i].y));
            }

        } else {
            for(int i = 0; i < numOfCards; i++ ) {
                cardPositions.add(new Vector2f(handPosition.x - cardPositionEven[Math.round((23-numOfCards)/2) + i].x,
                                                handPosition.y - cardPositionEven[Math.round((23-numOfCards)/2) + i].y));
            }
        }

        return cardPositions;
    }


    public ArrayList<Entity> getPickedCards() {
        return pickedCards;
    }


    public Card getTrumpCard() {
        return trumpCard;
    }

    public void setTrumpCard(Card trumpCard) {
        this.trumpCard= trumpCard;
    }


    /* Vrstvy stolu */
    public Entity getBackgroundLayer() {
        return backgroundLayer;
    }

    public void setBackgroundLayer(Entity tableEntity) {
        this.backgroundLayer = tableEntity;
    }

    public Entity getInfoLayer() {
        return infoLayer;
    }

    public void setInfoLayer(Entity infoEntity) {
        this.infoLayer = infoEntity;
    }


    /* Entity */
    public ArrayList<Entity> getCardsEntities() {
        return cardsEntities;
    }

    public ArrayList<Entity> getHidCardsEntities() {
        return hidCardsEntities;
    }


    public ArrayList<Card> getCards() {
        return cards;
    }


    public Entity getHandEntity() {
        return handEntity;
    }

    public void setHandEntity(Entity handEntity) {
        this.handEntity = handEntity;
    }


    public Entity getFrameEntity() {
        return frameEntity;
    }

    public void setFrameEntity(Entity frameEntity) {
        this.frameEntity = frameEntity;
    }

    public int getWait() {
        return wait;
    }

    public void setWait(int wait) {
        this.wait = wait;
    }






}
    



    
//
//    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
//        /* Vykresleni hraciho stolu (pozadi) */
//        backgroundLayer.render(gc, null, gr);
//    }
//
//    public void update(GameContainer gc, StateBasedGame sb, int delta) {
//        /* Vykresleni hraciho stolu (pozadi) */
//        backgroundLayer.update(gc,null,delta);
//    }
//}
